package cn.piaoyi.service.imp;

import java.util.*;

import org.apache.commons.logging.*;

import cn.piaoyi.bean.Cart;
import cn.piaoyi.bean.OrderGoods;
import cn.piaoyi.bean.OrderInfo;
import cn.piaoyi.bean.UserInfo;
import cn.piaoyi.comm.PiaoyiUtil;
import cn.piaoyi.dao.OrderGoodsDAO;
import cn.piaoyi.dao.OrderInfoDAO;
import cn.piaoyi.dao.UserInfoDAO;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.OrderInfoService;
import cn.piaoyi.service.web.PageList;
import cn.piaoyi.service.web.Pages;


public class OrderInfoServiceImp implements OrderInfoService {

	private static final Log logger = LogFactory.getLog(OrderInfoServiceImp.class);
	
	private OrderInfoDAO orderInfoDAO;
	private OrderGoodsDAO orderGoodsDAO;
	private UserInfoDAO userInfoDAO;

	public OrderInfoDAO getOrderInfoDAO() {
		return orderInfoDAO;
	}

	public void setOrderInfoDAO(OrderInfoDAO orderInfoDAO) {
		this.orderInfoDAO = orderInfoDAO;
	}
	
	public OrderGoodsDAO getOrderGoodsDAO() {
		return orderGoodsDAO;
	}

	public void setOrderGoodsDAO(OrderGoodsDAO orderGoodsDAO) {
		this.orderGoodsDAO = orderGoodsDAO;
	}

	public UserInfoDAO getUserInfoDAO() {
		return userInfoDAO;
	}

	public void setUserInfoDAO(UserInfoDAO userInfoDAO) {
		this.userInfoDAO = userInfoDAO;
	}

	/**
	 * 保存OrderInfo对象
	 * 
	 * @param orderInfo
	 * 			OrderInfo对象
	 * @return OrderInfo
	 * 			OrderInfo对象
	 */
	public OrderInfo createOrderInfo(OrderInfo orderInfo,List orderGoodses) throws PiaoyiException{
		try {
			orderInfo = this.getOrderInfoDAO().saveOrderInfo(orderInfo);
			if(orderGoodses == null || orderGoodses.isEmpty()){
				
			}else{
				for(int i=0;i<orderGoodses.size();i++){
					OrderGoods og = (OrderGoods) orderGoodses.get(i);
					og.setOrderId(orderInfo.getId());
					this.getOrderGoodsDAO().saveOrderGoods(og);
				}
			}
			return orderInfo;
		} catch (Exception e) {
			logger.error(e);
			throw new PiaoyiException(e);
		}
	}
	
	/**
	 * 保存OrderInfo对象
	 * 
	 * @param orderInfo
	 * 			OrderInfo对象
	 * @return OrderInfo
	 * 			OrderInfo对象
	 */
	public OrderInfo updateOrderInfo(OrderInfo orderInfo) throws PiaoyiException{
		try {
			orderInfo = this.getOrderInfoDAO().updateOrderInfo(orderInfo);
			return orderInfo;
		} catch (Exception e) {
			logger.error(e);
			throw new PiaoyiException(e);
		}
	}
	
	/**
	 * 根据主键ID获取OrderInfo对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return OrderInfo
	 * 			OrderInfo对象
	 */
	public OrderInfo findOrderInfoById(long id){
		OrderInfo oi = this.getOrderInfoDAO().findOrderInfoById(id);
		if(oi == null){
			return null;
		}else{
			List l = this.getOrderGoodsDAO().findOrderGoodses(oi.getId(), "id", 0);
			if(l == null || l.isEmpty()){
				return oi;
			}else{
				for(int i=0;i<l.size();i++){
					OrderGoods og = (OrderGoods) l.get(i);
					og.setTotalPrice(og.getPrice() * og.getGoodsNumber());
				}
				oi.setOrderGoodses(l);
				UserInfo ui = this.getUserInfoDAO().findUserInfoById(oi.getUserId());
				oi.setUserInfo(ui);
				return oi;
			}
		}
	}
	
	/**
	 * 根据主键orderNO获取OrderInfo对象
	 * 
	 * @param String
	 * 			订单号
	 * @return OrderInfo
	 * 			OrderInfo对象
	 */
	public OrderInfo findOrderInfoByOrderNO(String orderNO){
		OrderInfo oi = this.getOrderInfoDAO().findOrderInfoByOrderNO(orderNO);
		if(oi == null){
			return null;
		}else{
			List l = this.getOrderGoodsDAO().findOrderGoodses(oi.getId(), "id", 0);
			if(l == null || l.isEmpty()){
				return oi;
			}else{
				for(int i=0;i<l.size();i++){
					OrderGoods og = (OrderGoods) l.get(i);
					og.setTotalPrice(og.getPrice() * og.getGoodsNumber());
				}
				oi.setOrderGoodses(l);
				return oi;
			}
		}
	}
	
	/**
	 * 获取OrderInfo列表
	 * 
	 * @param userId
	 * 			long
	 * @param orderStatus
	 * 			int
	 * @param shippingStatus
	 * 			int
	 * @param payStatus
	 * 			int
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @param pages
	 * 			  Pages
	 * @return PageList
	 * 			OrderInfo对象列表
	 */
	public PageList findOrderInfos(long userId,int orderStatus,int shippingStatus,int payStatus,String orderby, int ascOrDesc,Pages pages){
		PageList pl = new PageList();
        if (pages.getTotalNum() == -1) {
            pages.setTotalNum(this.getOrderInfoDAO().getOrderInfoNum(userId, orderStatus, shippingStatus, payStatus));
        }
        pages.executeCount();
        if(pages.getTotalNum() == 0){
        	pl.setObjectList(null);
        	pl.setPages(pages);
        	return pl;
        }
		List l = this.getOrderInfoDAO().findOrderInfos(userId, orderStatus, shippingStatus, payStatus,orderby, ascOrDesc,pages.getSpage(), pages.getPerPageNum());
		if(l == null || l.isEmpty()){
	   		 pl.setObjectList(null);
	   	     pl.setPages(pages);
	   		 return pl;
		}else{
			pl.setObjectList(l);
	        pl.setPages(pages);
	        return pl;
		}
	}
	
	/**
	 * 删除OrderInfo对象
	 */
	public void removeOrderInfoById(long id){
		this.getOrderInfoDAO().removeOrderInfoById(id);
	}
}
